#include "mplayer.h"
#include"list.h"
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>


static int  fifo_fd;
static int  mplayer_flag;
static char mplayer_cmd[256];

// 0、打开管道文件，让命令得以从此管道输入 
int MPLAYER_OpenFifo(void)
{
	//0.判断文件是否存在
	//存在   -->  0  --> 假  -->if的内容不执行
	//不存在 --> -1  --> 真  -->if的内容执行
	if(access(MPLAYER_PATH,F_OK) == -1)  
	{
		//1.创建管道文件c
		int ret = mkfifo(MPLAYER_PATH,0777);
		if(ret == -1)
		{
			printf("mkfifo error!\n");
			return -1;
		}	
	}

	//2.访问管道文件
	fifo_fd = open(MPLAYER_PATH,O_RDWR);
	if(fifo_fd < 0)
	{
		printf("open fifo error!\n");
		return -2;
	}

	return 0;
}

// 1、关闭管道
void MPLAYER_CloseFifo(void)
{
	close(fifo_fd);
}

// 2、播放音视频
void MPLAYER_Play(int x, int y, int w, int h, char * mplayer_path)
{
	if(mplayer_flag == 0)	// 只让其执行一次
	{
		bzero(mplayer_cmd, sizeof(mplayer_cmd)); //清0
		sprintf(mplayer_cmd, "mplayer -slave -quiet -input file=%s -geometry %d:%d -zoom -x %d -y %d %s &", 
			MPLAYER_PATH, x, y, w, h, mplayer_path);
		system(mplayer_cmd);
		mplayer_flag = 1;
	}
}

// 3、音视频音量
void MPLAYER_AdjustVolume(int movie_volume_num)
{
	bzero(mplayer_cmd, sizeof(mplayer_cmd));
	sprintf(mplayer_cmd, "volume %d\n", movie_volume_num);	// -12到12
	write(fifo_fd,mplayer_cmd,strlen(mplayer_cmd));
}

// 4、音视频快进、快退
void MPLAYER_ForwardBehind(int movie_fw_bh_num)
{
	bzero(mplayer_cmd, sizeof(mplayer_cmd));
	sprintf(mplayer_cmd, "seek %d\n", movie_fw_bh_num);	  // 比如：负整数就是后退， 正整数快进
	write(fifo_fd,mplayer_cmd,strlen(mplayer_cmd));
}


//5、音视频暂停/继续
void MPLAYER_Stop(void)
{
	bzero(mplayer_cmd, sizeof(mplayer_cmd));
	strcpy(mplayer_cmd, "pause\n");
	write(fifo_fd,mplayer_cmd,strlen(mplayer_cmd));

}

//6、音视频退出
void MPLAYER_Quit(void)
{
	bzero(mplayer_cmd, sizeof(mplayer_cmd));
	strcpy(mplayer_cmd, "quit 0\n");
	write(fifo_fd,mplayer_cmd,strlen(mplayer_cmd));
	mplayer_flag = 0;
}

//7、视频静音设置1为静音开启，0为关闭
void MPLAYER_Mute(int mute_num)
{
	bzero(mplayer_cmd, sizeof(mplayer_cmd));
	sprintf(mplayer_cmd, "mute %d\n", mute_num);
	write(fifo_fd,mplayer_cmd,strlen(mplayer_cmd));
}


int MPLAYER_Movie(void)
{
	// 1、打开视频控制管道文件
	MPLAYER_OpenFifo();

	// 2、
	while(1)
    {
        int cmd = 0;
        char buf[1024] = {0};
		show_bmp_new(0,0,"./picture_file/movie.bmp");

		//这里获取相应的cmd来控制视频播放
        
        switch (cmd)
        {
            case 1:
				if (mplayer_flag == 0)
				{
					bzero(buf, sizeof(buf));
					printf("请输入要播放的视频:\n");
					scanf("%s", buf);
					while(getchar() != '\n');
					MPLAYER_Play(100, 100, 400, 240, buf);
				}
				else
					printf("视频正在播放中.......\n");
                break;

            case 2:
			    MPLAYER_ForwardBehind(-15) ; //快退
                break;

            case 3:
				MPLAYER_ForwardBehind(15) ;//快进
                break;

            case 4:
				MPLAYER_Stop();
				break;
			case 5:
			{
				int nute_num=-1;
				//加个触屏获取来控制

				MPLAYER_Mute(nute_num);
			}
			case 6:			
                goto exit_system_label;
			default:
				break;	
        }

    }
exit_system_label:
	// 关闭视频控制管道文件
	MPLAYER_Quit();
	MPLAYER_CloseFifo();


	return 0;
}









